package com.project2.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project2.bean.CommonData;
import com.project2.bean.PageBo;

import com.project2.bean.bo.CommentBo;
import com.project2.bean.po.MarketOrder;
import com.project2.mapper.MarketCommentMapper;

import com.project2.bean.bo.OrderDBO;
import com.project2.bean.po.*;
import com.project2.bean.vo.OrderDetailUserVO;
import com.project2.bean.vo.OrderDetailVO;
import com.project2.mapper.MarketGoodsMapper;
import com.project2.mapper.MarketOrderGoodsMapper;

import com.project2.mapper.MarketOrderMapper;
import com.project2.mapper.MarketUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;

import java.util.Date;
import java.util.List;


@Service
public class OrderMServiceImpl implements OrderMService{


    @Autowired
    MarketOrderMapper orderMapper;

    @Autowired
    MarketCommentMapper commentMapper;
    @Autowired
    MarketOrderGoodsMapper orderGoodsMapper;


    @Autowired
    MarketUserMapper userMapper;



    @Override
    public CommonData orderMInfo(PageBo pageBo, Date start, Date end, Short[] orderStatus, Integer userId, String orderSn) {
        PageHelper.startPage(pageBo.getPage(),pageBo.getLimit());

        MarketOrderExample example = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);

        if (start == null && end == null && orderStatus == null && userId == null && orderSn == "null"){
            List<MarketOrder> orderList = orderMapper.selectByExample(example);

            PageInfo<MarketOrder> pageInfo = new PageInfo<>(orderList);
            return CommonData.data(pageInfo);
        }



        //当有很多状态时 ，进行单独处理    创一个总list  查找到每个单的状态的listByStatus装入总list中

        if (orderStatus != null) {
            List<MarketOrder> list= new ArrayList<>();
             for (Short status : orderStatus) {
                if (status == null && "".equals(status)) {
                    continue;
                }

                 MarketOrderExample exampleST = new MarketOrderExample();

                 MarketOrderExample.Criteria criteriaST = exampleST.createCriteria();
                 criteriaST.andDeletedEqualTo(false);
                 if (start != null && end != null) {
                     criteriaST.andAddTimeBetween(start, end);
                 }


                 if (userId != null){
                     criteriaST.andUserIdEqualTo(userId);
                 }

                 if (orderSn != "null"){
                     criteriaST.andOrderSnEqualTo(orderSn);
                 }


                 criteriaST.andOrderStatusEqualTo(status);
                 List<MarketOrder> listByStatus = orderMapper.selectByExample(exampleST);

                 for (MarketOrder o : listByStatus) {
                     list.add(o);
                 }

            }


            PageInfo<MarketOrder> pageInfo = new PageInfo<>(list);
             return CommonData.data(pageInfo);
        }


        if (start != null && end != null) {
            // criteria.andAddTimeBetween(start, end);
            criteria.andPayTimeBetween(start,end);
        }


        if (userId != null){
            criteria.andUserIdEqualTo(userId);
        }

        if (orderSn != "null"){
            criteria.andOrderSnEqualTo(orderSn);
        }




        List<MarketOrder> orderList = orderMapper.selectByExample(example);

        PageInfo<MarketOrder> pageInfo = new PageInfo<>(orderList);
        return CommonData.data(pageInfo);

    }


    /**
     * 删除订单  应该逻辑删除 只把delete status  1
     * @param orderId
     * @return
     */
    @Override
    public int orderDelete(Integer orderId) {

        //物理删除
        // int num = orderMapper.deleteByPrimaryKey(orderId);

        //方法一 使用example
        //逻辑删除
        // MarketOrderExample example = new MarketOrderExample();
        // MarketOrderExample.Criteria criteria = example.createCriteria();
        // criteria.andIdEqualTo(orderId);

        MarketOrder order = new MarketOrder();
        order.setDeleted(true);
        order.setId(orderId);

        //方法二：
        // int num = orderMapper.updateByExampleSelective(order, example);

        int num = orderMapper.updateByPrimaryKeySelective(order);

        return num;
    }



    @Override
    public int orderRefund(OrderDBO order) {

        MarketOrderExample example = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(order.getOrderId());

        MarketOrder orderUpdate = new MarketOrder();
        orderUpdate.setOrderStatus((short) 203);
        orderUpdate.setRefundAmount(order.getRefundMoney());
        orderUpdate.setRefundType("微信退款接口");
        orderUpdate.setUpdateTime(new Date());

        int num = orderMapper.updateByExampleSelective(orderUpdate,example);

        return num;
    }

    @Override
    public List<MarketChannel> getChannel() {

        List<MarketChannel> list = orderMapper.selectAll();

        return list;
    }

    @Override
    public int orderShip(Integer orderId, String shipChannel, String shipSn) {

        MarketOrderExample example = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(orderId);


        MarketOrder order = new MarketOrder();
        order.setShipChannel(shipChannel);
        order.setShipSn(shipSn);
        order.setShipTime(new Date());
        order.setUpdateTime(new Date());
        order.setOrderStatus((short) 301);

        int num = orderMapper.updateByExampleSelective(order, example);

        return num;
    }

    @Override
    public OrderDetailVO orderDetail(Integer id){

        MarketOrder order = orderMapper.selectByPrimaryKey(id);

        MarketOrderGoodsExample example = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(order.getId());
        List<MarketOrderGoods> marketOrderGoods = orderGoodsMapper.selectByExample(example);


        Integer userId = order.getUserId();
        MarketUser user = userMapper.selectByPrimaryKey(userId);

        OrderDetailUserVO userVO = new OrderDetailUserVO(user.getAvatar(), user.getNickname());

        return new OrderDetailVO(order,marketOrderGoods,userVO);
    }

    @Override
    public int addContent(CommentBo commentBo) {
        Date date =new Date();
        Integer commentId = commentBo.getCommentId();
        String content = commentBo.getContent();

        int i = commentMapper.updateContentByCommentId(commentId, content, date);
        return 0;
    }
}
